#region License and Copyright

/*
 * Copyright (C) 2008 Nathan Palmer
 * http://www.nathanpalmer.com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author:
 *     Nathan Palmer <nathan1@nathanpalmer.com>
 */

#endregion

using System;
using System.Xml;
using Microsoft.Web.Services2;
using System.Collections;

namespace Amazon
{
    #region HeaderOutputFilter

    // Used to filter header nodes.
    public class HeaderOutputFilter : SoapOutputFilter
    {
        private string filterString;

        public HeaderOutputFilter(string filterString)
        {
            this.filterString = filterString;
        }

        // Override the ProcessMessage method.
        public override void ProcessMessage(SoapEnvelope envelope)
        {
            if (envelope == null)
                throw new ArgumentNullException("No envelope!");

            if (envelope.Header != null)
            {
                XmlNodeList nodeList = envelope.Header.ChildNodes;

                ArrayList removeList = new ArrayList();

                foreach (XmlNode node in nodeList)
                {
                    if (node is XmlElement)
                    {
                        // Check for any headers that belong to WSE.
                        if (node.Name.IndexOf(this.filterString) == 0)
                        {
                            removeList.Add(node);
                        }
                    }
                }
                // Remove headers. 
                foreach (XmlNode node in removeList)
                {
                    envelope.Header.RemoveChild(node);
                }
            }
        }
    }

    #endregion
}
